package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.AddressService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("address")
public class AddressController {
    @DubboReference
    private AddressService addressService;

    @RequestMapping("getAddress")
    @ResponseBody
    public AjaxResult getAddress(){

        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        String username = user.getUsername();
        if (username == null && username.equals("") && username.equals("null")){
            return AjaxResult.no();
        }
        List<TbAddress> list = addressService.findList(username);
        return AjaxResult.ok().setData(list);
    }

    //省
    @RequestMapping("getProvince")
    @ResponseBody
    private AjaxResult toAdd(Integer provinceId){

        List<TbProvinces> provincesList = addressService.selectProvince(provinceId);

        return AjaxResult.ok().setData(provincesList);
    }

    //市
    @RequestMapping("getCities")
    @ResponseBody
    private AjaxResult getCities(Integer provinceId){

        List<TbCities> citiesList = addressService.selectCities(provinceId);

        return AjaxResult.ok().setData(citiesList);
    }

    //县
    @RequestMapping("getAreas")
    @ResponseBody
    private AjaxResult getAreas(Integer citiesId){

        List<TbAreas> areasList = addressService.selectAreas(citiesId);

        return AjaxResult.ok().setData(areasList);
    }

    //地址增加
    @RequestMapping("saveInfo")
    @ResponseBody
    private AjaxResult saveInfo(TbAddress address){
        TbUser user =(TbUser) SecurityUtils.getSubject().getPrincipal();
        String username = user.getUsername();
        address.setUserId(username);
        Boolean b = addressService.saveInfo(address);
        return AjaxResult.ok().setData(b);
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public AjaxResult deleteInfo(Long id){

        Boolean b = addressService.deleteInfo(id);

        return AjaxResult.ok().setData(b);
    }
}
